//
//  MANaviRoute.m
//  OfficialDemo3D
//
//  Created by yi chen on 1/7/15.
//  Copyright (c) 2015 songjian. All rights reserved.
//

#import "MANaviRoute.h"
#import "CommonUtility.h"

#define kMANaviRouteReplenishPolylineFilter     8

@interface MANaviRoute()

@property (nonatomic, assign) BOOL anntationVisible;
@property (nonatomic, weak) MAMapView *mapView;

@end

@implementation MANaviRoute

- (void)addToMapView:(MAMapView *)mapView
{
    self.mapView = mapView;
    
    if ([self.routePolylines count] > 0)
    {
        [mapView addOverlays:self.routePolylines];
    }
    
    if (self.anntationVisible && [self.naviAnnotations count] > 0)
    {
        [mapView addAnnotations:self.naviAnnotations];
    }
}

- (void)removeFromMapView
{
    if (self.mapView == nil)
    {
        return;
    }
    
    if ([self.routePolylines count] > 0)
    {
        [self.mapView removeOverlays:self.routePolylines];
    }
    
    if (self.anntationVisible && [self.naviAnnotations count] > 0)
    {
        [self.mapView removeAnnotations:self.naviAnnotations];
    }
    
    self.mapView = nil;
}




/* polyline parsed from search result. */

+ (MAPolyline *)polylineForStep:(AMapStep *)step
{
    if (step == nil)
    {
        return nil;
    }
    
    return [CommonUtility polylineForCoordinateString:step.polyline];
}

+ (MAPolyline *)polylineForBusLine:(AMapBusLine *)busLine
{
    if (busLine == nil)
    {
        return nil;
    }
    
    return [CommonUtility polylineForCoordinateString:busLine.polyline];
}


#pragma mark - Life Cycle

+ (instancetype)naviRouteForPath:(AMapPath *)path withNaviType:(MANaviAnnotationType)type andAnnotations:(NSArray *)annotations
{
    return [[self alloc] initWithPath:path withNaviType:type andAnnotations:annotations];
}

- (instancetype)initWithPath:(AMapPath *)path withNaviType:(MANaviAnnotationType)type andAnnotations:(NSArray *)annotations
{
    self = [self init];
    
    if (self == nil)
    {
        return nil;
    }
    
    if (path == nil || path.steps.count == 0)
    {
        return self;
    }
    
    NSMutableArray *polylines = [NSMutableArray array];
  
    [path.steps enumerateObjectsUsingBlock:^(AMapStep *step, NSUInteger idx, BOOL *stop) {
        
        MAPolyline *stepPolyline = [MANaviRoute polylineForStep:step];
        
        if (stepPolyline != nil)
        {
            MANaviPolyline *naviPolyline = [[MANaviPolyline alloc] initWithPolyline:stepPolyline];
            naviPolyline.type = type;
            [polylines addObject:naviPolyline];
        }
    }];
    
    NSMutableArray *naviAnnotations = [NSMutableArray array];
    [annotations enumerateObjectsUsingBlock:^(MANaviAnotationWayModel *step, NSUInteger idx, BOOL *stop) {
       
        
        if (![step.extension isEqualToString:@"5"]) {
            MANaviAnnotation * annotation = [[MANaviAnnotation alloc] init];
            annotation.coordinate =   CLLocationCoordinate2DMake(step.latitude, step.longitude);
            annotation.title = step.title;
            annotation.type  = MANaviAnnotationTypeDrive;
            annotation.extension = step.extension;
            [naviAnnotations addObject:annotation];
        }
        
      
    }];
   
    self.routePolylines = polylines;
    self.naviAnnotations = naviAnnotations;
    
    return self;
}


- (id)init
{
    self = [super init];
    if (self)
    {
        self.anntationVisible = YES;
        self.routeColor   = [UIColor colorWithRed:62.0/255.0 green:152.0/255.0 blue:225.0/255.0 alpha:1.0];
        self.walkingColor = [UIColor orangeColor];
    }
    
    return self;
}

@end
